package emergency;

import java.util.ArrayList;

import unit.*;


public class RequirementsFacilityFire extends RequirementsEmergency {

	/**
	 * Checks if this requirement is the right one to handle the given emergency
	 * 
	 * @param emergency
	 *            the emergency to check if this Requirement needs to handle it
	 * 
	 * @return Returns true if this is the right requirement for the given
	 *         emergency, false if not
	 */
	@Override
	boolean canHandleEmergency(Emergency emergency) {
		boolean canHandle = false;
		if (emergency instanceof Fire){
			Fire fire = (Fire) emergency;
			if(fire.getSize().equals(Fire.Size.FACILITY)){
				canHandle = true;
			}
		}
		return canHandle;
	}

	
	/**
	 * Method that will create the suggestion for the given emergency, with the
	 * given units
	 * 
	 * @param emergency
	 *            the emergency we need to compute the suggestion for
	 * 
	 * @param units
	 *            list of units of which the needed units will be picked
	 * 
	 * @return Returns a list of suggested units to assign to the emergency
	 * 
	 * @throws IllegalArgumentException
	 *             not enough units are available in the given unit list
	 */
	@Override
	ArrayList<Unit> createSuggestion(Emergency emergency, ArrayList<Unit> units) {
		if(canHandleEmergency(emergency)){
			ArrayList<Unit> suggestedUnits = new ArrayList<Unit>();
				setRequirements(emergency);

				int amb = getTotalAmbulancesNeeded();
				int pol = getTotalPoliceCarsNeeded();
				int fir = getTotalFireTrucksNeeded();
				
				for (Unit u : units){
					if(u instanceof FireTruck){
						FireTruck fireTruck = (FireTruck) u;
						if(fir > 0 && fireTruck.getMaxFireSize().equals(Fire.Size.FACILITY)){
							suggestedUnits.add(fireTruck);
							fir--;
						}
					}
					
					if(u instanceof PoliceCar){
						if(pol > 0){
							suggestedUnits.add(u);
							pol--;
						}
					}
					
					if(u instanceof Ambulance){
						if(amb > 0){
							suggestedUnits.add(u);
							amb --;
						}
					}
				}
			emergency.setEmergencyRequirements(this);		
			
			if(amb == 0 && fir == 0 && pol == 0)
				return suggestedUnits;
			
			
			else throw new IllegalArgumentException("Not enough units given to handle this emergency");
		}
		else{
			return getNextInChain().createSuggestion(emergency, units);
		}
	}

	/**
	 * Method for setting the total units needed, depending on this requirement
	 * 
	 * @post The total amount of ambulances needed is set, depending on this
	 *       requirement
	 * 
	 * @post The total amount of policecars needed is set, depending on this
	 *       requirement
	 * 
	 * @post The total amount of firetrucks needed is set, depending on this
	 *       requirement
	 * 
	 * @post The total amount of units needed is set, depending on this
	 *       requirement
	 */
	@Override
	void setRequirements(Emergency emergency) {
		if(canHandleEmergency(emergency)){
				emergency.setEmergencyRequirements(this);
				Fire fire = (Fire) emergency;
				
				setTotalFireTrucksNeeded(4);
				setTotalPoliceCarsNeeded(3);
				setTotalAmbulancesNeeded(fire.getTrappedPeople() + fire.getNumberOfInjured());
				
				int alreadyAssigned = amountOfAssignedUnits("PoliceCar",emergency.getUnitsClone());
				setTotalPoliceCarsNeeded(diff(getTotalPoliceCarsNeeded(),alreadyAssigned));

				alreadyAssigned = amountOfAssignedUnits("Ambulance",emergency.getUnitsClone());
				setTotalAmbulancesNeeded(diff(getTotalAmbulancesNeeded(),alreadyAssigned));
				
				alreadyAssigned = amountOfAssignedUnits("FireTruck",emergency.getUnitsClone());
				setTotalFireTrucksNeeded(diff(getTotalFireTrucksNeeded(),alreadyAssigned));

				setTotalUnitsNeeded(getTotalAmbulancesNeeded() + getTotalFireTrucksNeeded() + getTotalPoliceCarsNeeded());
		}
		else getNextInChain().setRequirements(emergency);
	}
}
